<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<!--- <cfif NOT IsDefined("client.funds.orderBy") or resetFilters>
	<cfset client.funds.orderBy = "Anticipated_Ship_Date">
<cfelseif IsDefined("orderBy")>
	<cfset client.funds.orderBy = orderBy>
</cfif>

<cfif NOT IsDefined("client.funds.orderDir") or resetFilters>
	<cfset client.funds.orderDir = "asc">
<cfelseif IsDefined("orderDir")>
	<cfset client.funds.orderDir = orderDir>
</cfif> --->

<cfif NOT IsDefined("client.funds.approved") or resetFilters>
	<cfset client.funds.approved = "All">
<cfelseif IsDefined("filterApproved")>
	<cfset client.funds.approved = filterApproved>
</cfif>
<cfif client.funds.approved neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Approved = client.funds.approved />
</cfif>

<cfif NOT IsDefined("client.funds.programYear") or resetFilters>
	<cfset client.funds.programYear = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYear")>
	<cfset client.funds.programYear = filterProgramYear>
</cfif>
<cfif client.funds.programYear neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.Program_Year = client.funds.programYear />
<cfelse>
	<cfset queryParams.Program_Year = request.currentProgramYear />
</cfif>

<cfif NOT IsDefined("client.funds.organizationIDs") or resetFilters>
	<cfset client.funds.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.funds.organizationIDs = filterOrganizationIDs>
</cfif>
<cfif client.funds.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.funds.organizationIDs />
</cfif>

<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.funds.regionIDs") or resetFilters>
		<cfset client.funds.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.funds.regionIDs = filterRegionIDs>
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.funds.regionIDs = request.authUser.regionID>
</cfif>
<cfif client.funds.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.funds.regionIDs />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.funds.queryParamsWDDX" />

<cfscript>
	fundGateway = new assets.calgrants.com.gateway.FundGateway();
	funds = fundGateway.query(argumentCollection = queryParams);
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
</cfscript>

<cfoutput>

	<h3>Fund Requests<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>

	<!--- <p>
		<a href="#script_name#?view=editFund&Order_ID=0" class="btn">New&nbsp;order</a> <a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a><!--- <cfif isFiltered> <a href="#script_name#?view=funds&resetFilters=true" class="btn">Clear filters</a></cfif> ---><cfif funds.RecordCount gt 0> <a href="?action=export&collection=funds" class="btn" target="_new">Export</a></cfif><cfif IsUserInRole("Super User")> <a href="?view=fundsSummary&Program_Year=#request.currentProgramYear#" class="btn" target="_new">Summary</a></cfif>
		<span class="pull-right"><strong>Count:</strong> #funds.RecordCount#</span>
	</p> --->
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a href="#script_name#?view=editFund&Fund_ID=0" class="btn">New&nbsp;fund&nbsp;request</a>
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif funds.RecordCount gt 0>
						<li><a href="?action=export&collection=funds" target="_new">Export</a></li>
					<cfelseif NOT IsUserInRole("Super User")>
						<li><a href="##">None - no records available with filter settings</a></li>
					</cfif>
					<!--- <cfif IsUserInRole("Super User")>
						<li><a href="?view=fundsSummary&Program_Year=#request.currentProgramYear#&printable=true" target="_new">#request.currentProgramYear# Summary Report</a></li>
					</cfif> --->
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #funds.RecordCount#</span>
		</div>
	</div>

	<cfform name="fundsForm" id="fundsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td>
						<label>Program Year</label>
						<cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2010" to="#ListFirst(application.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.funds.programYear eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect>
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.funds.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.funds.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.funds.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
							<option value="#Organization_ID#"<cfif ListFind(client.funds.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Status</label>
						<cfselect name="filterApproved" size="1" multiple="false" required="yes" message="Status filter is required" style="width: 150px;">
							<option value="All"<cfif client.funds.approved eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.funds.approved eq "Yes"> selected</cfif>>Approved</option>
							<option value="No"<cfif client.funds.approved eq "No"> selected</cfif>>Not approved</option>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=funds&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif funds.RecordCount gt 0>

			<table id="fundsList" style="display: none; width: 100%">
				<thead>
					<th>ID</th>
					<th>Program Year</th>
					<th>Organization</th>
					<cfif IsUserInRole("Super User")>
						<th>Region</th>
					</cfif>
					<th>Request Date</th>
					<th>Requested By</th>
					<th>Workshops</th>
					<th>Appeals</th>
					<th>Amount</th>
					<th>Approval Date</th>
					<th>Status</th>
				</thead>
				<tbody>
					<cfloop query="funds">
						<tr>
							<td>
								<!--- <cfif Program_Year gte "2012-2013">
									<cfif IsUserInRole("Super User")> --->
										<a href="#script_name#?view=editFund&Fund_ID=#Fund_ID#">#Fund_ID#</a>
									<!--- <cfelse>
										<a href="#script_name#?view=fund&Fund_ID=#Fund_ID#">#Fund_ID#</a>
									</cfif>
								<cfelse>
									#Fund_ID#
								</cfif>	 --->					
							</td>
							<td>#Program_Year#</td>
							<td>
								<cfif Organization_Deleted>
									<font color="red">#Organization#</font>
								<cfelseif Organization_Archived>
									<font color="silver">#Organization#</font>
								<cfelseif NOT Organization_Approved>
									<font color="orange">#Organization#</font>
								<cfelseif Organization_Approved>
									#Organization#
								</cfif>
							</td>
							<cfif IsUserInRole("Super User")>
								<td>#Region_ID#</td>
							</cfif>
							<td><span class="sortHide">#DateFormat(Date_Created, 'yyyymmdd')#</span>#DateFormat(Date_Created, "m/d/yyyy")#</td>
							<td>
								<cfif Requested_By_Deleted>
									<font color="red">#Requested_By_Full_Name#</font>
								<cfelseif Requested_By_Archived>
									<font color="silver">#Requested_By_Full_Name#</font>
								<cfelseif NOT Requested_By_Approved>
									<font color="orange">#Requested_By_Full_Name#</font>
								<cfelseif Requested_By_Approved>
									#Requested_By_Full_Name#
								</cfif>
							</td>
							<td>#Fund_Workshop_Count#</td>
							<td><cfif Fund_Workshop_Appeal_Count gt 0>#Fund_Workshop_Appeal_Reviewed_Count# of </cfif>#Fund_Workshop_Appeal_Count#<cfif Fund_Workshop_Appeal_Count gt 0> reviewed</cfif></td>
							<td>
								<cfif IsDate(Approved_Date)>
									#DollarFormat(Fund_Amount_Total)#
								</cfif>
							</td>
							<td>
								<cfif IsDate(Approved_Date)>
									<span class="sortHide">#DateFormat(Approved_Date, 'yyyymmdd')#</span>#DateFormat(Approved_Date, "m/d/yyyy")#
								</cfif>
							</td>
							<td>
								<cfif Deleted>
									<font color="red">Deleted</font>
								<cfelseif NOT Approved>
									<font color="orange">Not Approved</font>
								<cfelseif Approved>
									Approved
								</cfif>
							</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##fundsList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						}			
					})
					$("##fundsList").show();
					var fundsListGrid = $("##fundsList").data("kendoGrid");
					fundsListGrid.bind("dataBound", function(e) {
						console.log(e);
					});
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No fund requests</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>
			
		</cfif>

	</cfform>

</cfoutput>